package com.spring;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class JedisSet {

    public static void test1(Jedis jedis) {
        System.out.println("sadd 和 smember 的使用");
        jedis.flushAll();

        jedis.sadd("key1","123","456","789");
        jedis.sadd("key2","123","456","153");

        System.out.println(jedis.smembers("key1"));
    }

    public static void test2(Jedis jedis) {
        System.out.println("scard 和 sismember 的使用");
        jedis.flushAll();

        jedis.sadd("key1","123","456","789");
        jedis.sadd("key2","123","456","153");

        System.out.println(jedis.scard("key1"));

        System.out.println(jedis.sismember("key2","123"));
    }

    public static void test3(Jedis jedis) {
        // 随机删除
        System.out.println("spop 的使用");
        jedis.flushAll();

        jedis.sadd("key1","123","456","789");

        String key1 = jedis.spop("key1");
        System.out.println(key1);

        key1 = jedis.spop("key1");
        System.out.println(key1);
    }

    public static void test4(Jedis jedis) {
        // 随机删除
        System.out.println("sinter 和 sinterstore 的使用");
        jedis.flushAll();

        jedis.sadd("key1","123","456","789");
        jedis.sadd("key2","123","456","153");

        Set<String> sinter = jedis.sinter("key1", "key2");
        System.out.println(sinter);

        long sinterstore = jedis.sinterstore("key3","key1", "key2");
        System.out.println(sinterstore);
        System.out.println(jedis.smembers("key3"));
    }

    public static void main(String[] args) {
        JedisPool jedispool=new JedisPool("tcp://127.0.0.1:8888");

        try(Jedis jedis=jedispool.getResource()){
            jedis.auth("123456");
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
            test4(jedis);
        }
    }
}
